package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.adm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.common.utils.BootstrapUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.AutoInfoMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.AutoInfo;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.IamFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.AutoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by yangsy on 2016/12/15.
 */

public class AutoInfoServiceImpl implements AutoInfoService {
    @Autowired
    private AutoInfoMapper autoInfoMapper;
    @Autowired
    private IamFileService iamFileService;
    @Override
    public int deleteByPrimaryKey(String autoId) {
        return autoInfoMapper.deleteByPrimaryKey(autoId);
    }

    @Override
    public int insert(AutoInfo record) {
        return autoInfoMapper.insert(record);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(AutoInfo record,String uploadFiles,String deletedFiles) {
        // 数据保存成功之后，返回的条数
        int resultCount = 0;
        // 保存业务数据
        if (null == record){
            return resultCount;
        }
        resultCount += autoInfoMapper.insertSelective(record);
        // 附件保存
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getAutoId());

        return resultCount;
    }

    @Override
    public AutoInfo selectByPrimaryKey(String autoId) {
        return autoInfoMapper.selectByPrimaryKey(autoId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(AutoInfo record,String uploadFiles,String deletedFiles) {
        // 数据保存成功之后，返回的条数
        int resultCount = 0;
        // 保存业务数据
        if (null == record){
            return resultCount;
        }
        resultCount += autoInfoMapper.updateByPrimaryKeySelective(record);
        // 附件保存
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getAutoId());

        return resultCount;
    }

    @Override
    public int updateByPrimaryKey(AutoInfo record) {
        return autoInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public JSONObject list(BootstrapSearchBean bootstrapSearchBean) {
        JSONObject jsonObject = new JSONObject();
        bootstrapSearchBean.setLimit( bootstrapSearchBean.getLimit() == null ? 10 : bootstrapSearchBean.getLimit() );
        bootstrapSearchBean.setOffset( bootstrapSearchBean.getOffset() == null ? 0 : bootstrapSearchBean.getOffset() );
        bootstrapSearchBean.setFilterMap(BootstrapUtil.filterStringToMap(bootstrapSearchBean.getFilter()));
        jsonObject.put("total",autoInfoMapper.queryCount(bootstrapSearchBean));
        jsonObject.put("rows", JSON.toJSON(autoInfoMapper.queryList(bootstrapSearchBean)));
        return jsonObject;
    }
}
